<?php

Class MessageManager
{
	private $_db;
	
	public function __construct(PDO $db)
	{
		$this->setDb($db);
	}
	
	public function setDb($db)
	{
		$this->_db = $db;
	}
	
	public function add(Message $message)
	{
		//Fonction qui va ajouter un message dans la table messages + envoyer une copie par mail si sendMail = 1
			//Ajout dans la BDD
		$reqInsert = $this->_db->prepare("INSERT INTO messages SET contest_id= :contest_id, messageDate= NOW(), responseMessage_id= :responseMessage_id, messageFrom= :messageFrom, mailFrom= :mailFrom, messageTo= :messageTo, mailTo= :mailTo, subject= :subject, text= :text, sendMail= :sendMail, inInbox= 1, category_id= :category_id");
		$reqInsert->bindValue(":contest_id", $message->contest_id());
		$reqInsert->bindValue(":responseMessage_id", $message->responseMessage_id());
		$reqInsert->bindValue(":messageFrom", $message->messageFrom());
		$reqInsert->bindValue(":mailFrom", $message->mailFrom());
		$reqInsert->bindValue(":messageTo", $message->messageTo());
		$reqInsert->bindValue(":mailTo", $message->mailTo());
		$reqInsert->bindValue(":subject", htmlentities($message->subject()));
		$reqInsert->bindValue(":text", $message->text());
		$reqInsert->bindValue(":category_id", $message->category_id());
		$reqInsert->bindValue(":sendMail", $message->sendMail());
		$reqInsert->execute();
		$message->hydrate(array(
			'id' => $this->_db->lastInsertId()
		));
			//Envoi du mail si nécessaire
		if($message->sendMail()==1)
		{
			$subject = utf8_decode($message->subject());
			$subject = mb_encode_mimeheader($subject,"UTF-8");
			$headers = 'MIME-Version: 1.0' . "\r\n";
			$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
			$headers .= 'From: Noreply Kilbet <noreply@kilbet.fr>' . "\r\n";
			mail($message->mailTo(), $subject, $message->text(), $headers);
		}
	}
	public function delete(Message $message)
	{
		//Fonction qui va supprimer un message dans la table messages
		$reqDelete = $this->_db->prepare("DELETE FROM messages WHERE id= :id");
		$reqDelete->bindValue(":id", $user->id());
		$reqDelete->execute();
	}
	public function updateInInbox(Message $message, $inbox)
	{
		//Fonction qui va mettre à jour un message dans la table messages : faire disparaître ce message de la boîte de réception du destinataire
		$reqUpdate = $this->_db->prepare("UPDATE messages SET messageDate= :messageDate, responseMessage_id= :responseMessage_id, messageFrom= :messageFrom, messageTo= :messageTo, mailTo= :mailTo, subject= :subject, text= :text, sendMail= :sendMail, inInbox= :inInbox, category_id= :category_id WHERE id= :id");
		$reqUpdate->bindValue(":id", $user->id(), PDO::PARAM_INT);
		$reqUpdate->bindValue(":messageDate", $message->messageDate(), PDO::PARAM_STR);
		$reqUpdate->bindValue(":responseMessage_id", $message->responseMessage_id(), PDO::PARAM_INT);
		$reqUpdate->bindValue(":messageFrom", $message->messageFrom(), PDO::PARAM_INT);
		$reqInsert->bindValue(":mailFrom", $message->mailFrom());
		$reqUpdate->bindValue(":messageTo", $message->messageTo(), PDO::PARAM_INT);
		$reqUpdate->bindValue(":mailTo", $message->mailTo(), PDO::PARAM_STR);
		$reqUpdate->bindValue(":subject", $message->subject(), PDO::PARAM_STR);
		$reqUpdate->bindValue(":text", $message->text(), PDO::PARAM_STR);
		$reqUpdate->bindValue(":sendMail", $message->sendMail(), PDO::PARAM_INT);
		$reqUpdate->bindValue(":inInbox", $inbox, PDO::PARAM_INT);
		$reqUpdate->bindValue(":category_id", $message->category_id(), PDO::PARAM_INT);
		$reqUpdate->execute();
	}
	public function get($search)
	{
		//Fonction qui va chercher un message dans la table messages à partir du critère de recherche search
		$search = (int) $search;
		if(is_int($search))
		{
			$reqSelect = $this->_db->prepare("SELECT * FROM messages WHERE id= :id");
			$reqSelect->bindValue(":id", $search);
			$reqSelect->execute();
			$message = $reqSelect->fetch(PDO::FETCH_ASSOC);
			return new Message($message);
		}
	}
	
	public function getCategory(Message $message)
	{
		//Fonction qui va chercher le libellé de la catégorie du message
		$reqSelect = $this->_db->prepare("SELECT name FROM messagecategories WHERE id IN(SELECT category_id FROM messages WHERE id = :idMessage)");
		$reqSelect->bindValue(":idMessage", $message->id());
		$reqSelect->execute();
		$category = $reqSelect->fetch(PDO::FETCH_ASSOC);
		return $category['name'];
	}
}

?>